SUFFIX = `date +%y%m%d`
INSTALL_PREFIX = /home/xtv2/
BINDALL = 0
CP_ADDR =127.0.0.1
SP_ADDR =127.0.0.1
TS_ADDR =127.0.0.1
CPXMLPATH = 
SPXMLPATH =
TSXMLPATH =
CC = g++ -g -Wall -DDEBUG -lstdc++
CP = mv -f
SRC = SPnew.c sessions.c timer.c job.c livechannel.c util.c md5.c db.c CPnew.c TSnew.c makeDB.c formatchange.c echo.h Makefile ErrorDefine.h StructDefine.h md5.h util.h ProtocolDefine.h asp.cfg gcp.cfg ats.cfg netblocks.cfg
CP_SRC = CPnew.c job.c livechannel.c util.c
SP_SRC = SPnew.c job.c livechannel.c util.c md5.c db.c
PLIST_SRC = SPnew.c job.c livechannel.c util.c md5.c db.c timer.c
TS_SRC = TSnew.c util.c findcp.cpp
TARGET = tsnew
RM = rm
#MYSQL = -ldb -lssl
DEFINE_TS = -static -DMEASUREMENT -DSO_LINGER -DSORT_NET -DTEST -DHAVE_RM
DEFINE_GCP = -static -D__CP_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DP2PS_PORT=50002 -DCP2TS_PORT=3947 -DCONFIG="\"./gcp.cfg\"" -DHAVE_TS -DSO_LINGER
DEFINE_SP = -static -D__SP_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DTEST -DSO_LINGER -I/usr/local/BerkeleyDB.4.3/include -L/usr/local/BerkeleyDB.4.3/lib -ldb
DEFINE_PLAYLISTSP = -static -D__HAVE_PLIST -D__SP_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DTEST -DSO_LINGER -I/usr/local/BerkeleyDB.4.3/include -L/usr/local/BerkeleyDB.4.3/lib -ldb
.IGNORE:
all: $(TARGET)	
tsnew:	$(TS_SRC)
	$(CC) -o $@.$(SUFFIX) $^ $(DEFINE_TS)
#spnew:	$(SP_SRC)
#	$(CC) -o  $@.$(SUFFIX) $^ $(DEFINE_SP)
spnew:	$(PLIST_SRC)
	$(CC) -o  $@.$(SUFFIX) $^ $(DEFINE_PLAYLISTSP)
gcpnew:	$(CP_SRC)
	$(CC) -o $@.$(SUFFIX) $^ $(DEFINE_GCP)
makeDB:	db.c makeDB.c md5.c
	$(CC) -o  $@ $^ $(DEFINE_SP) -DDB_DIR="\"$(INSTALL_PREFIX)/db/\""
fchange: formatchange.c
	$(CC) -o $@ $^
install: all
	mkdir $(INSTALL_PREFIX)
	mkdir $(INSTALL_PREFIX)/db
	$(CP) $(TARGET) netblocks.cfg $(INSTALL_PREFIX)
	IP=`ifconfig eth0 | grep -oe 'inet addr:[0-9.]*'| sed -e 's/inet addr://'`;\
	if test -z $(TS_ADDR) ; then TS_ADDR=$$IP; else TS_ADDR=$(TS_ADDR);fi;\
	echo "TS address is" $$TS_ADDR;\
	if test -z $(CP_ADDR) ; then CP_ADDR=$$IP; else CP_ADDR=$(CP_ADDR);fi;\
	echo "CP address is" $$CP_ADDR;\
	if test -z $(SP_ADDR) ; then SP_ADDR=$$IP; else SP_ADDR=$(SP_ADDR);fi;\
	echo "SP address is" $$SP_ADDR; \
	sed "1,$$ s/(TS_ADDR)/$$TS_ADDR/g; 1,$$ s/(CP_ADDR)/$$CP_ADDR/g; 1,$$ s/(SP_ADDR)/$$SP_ADDR/g; 1,$$ s/(TSXMLPATH)/$$TSXMLPATH/g" ats.cfg > $(INSTALL_PREFIX)/ats.cfg; \
	sed "1,$$ s/(TS_ADDR)/$$TS_ADDR/g; 1,$$ s/(CP_ADDR)/$$CP_ADDR/g; 1,$$ s/(SP_ADDR)/$$SP_ADDR/g; 1,$$ s/(SPXMLPATH)/$$SPXMLPATH/g" asp.cfg > $(INSTALL_PREFIX)/asp.cfg; \
	sed "1,$$ s/(TS_ADDR)/$$TS_ADDR/g; 1,$$ s/(CP_ADDR)/$$CP_ADDR/g; 1,$$ s/(SP_ADDR)/$$SP_ADDR/g; 1,$$ s/(CPXMLPATH)/$$CPXMLPATH/g" gcp.cfg > $(INSTALL_PREFIX)/gcp.cfg;
	echo "BINDALL=$(BINDALL)" >> $(INSTALL_PREFIX)/ats.cfg; \
	echo "BINDALL=$(BINDALL)" >> $(INSTALL_PREFIX)/asp.cfg;
dist-src:
	mkdir chaos_$(SUFFIX); cp $(SRC) chaos_$(SUFFIX); tar czf chaos_$(SUFFIX).tgz chaos_$(SUFFIX);rm -fr chaos_$(SUFFIX);
dist-bin:	all
	tar czf chaosbin_$(SUFFIX).tgz tsnew.$(SUFFIX) gcpnew.$(SUFFIX) spnew.$(SUFFIX) fchange makeDB
clean: 
	$(RM) $(TARGET) *.$(SUFFIX)
